﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Dominio
{
    public class Plugin
    {
        private String dllPathFile;

        internal String DllPathFile
        {
            get { return dllPathFile; }
            set { dllPathFile = value; }
        }

        private IPlugin pluginInstance;

        private IPlugin PluginInstance
        {
            get { return pluginInstance; }
            set { pluginInstance = value; }
        }

        public Plugin(String dllPathFile)
        {
            DllPathFile = dllPathFile;
            PluginInstance = CrearInstanciaIPlugin();
        }

        private IPlugin CrearInstanciaIPlugin()
        {
            try
            {
                Assembly a = Assembly.LoadFrom(@DllPathFile);
                Type iPlugInImplementation = null;
                foreach (Type t in a.GetTypes())
                {
                    if (t.GetInterface("Dominio.IPlugin") != null && t.IsAbstract == false)
                    {
                        iPlugInImplementation = t;

                        break;
                    }
                }

                return ((IPlugin)Activator.CreateInstance(iPlugInImplementation));
            }
            catch (ArgumentNullException)
            {
                throw new Dominio.Exceptions.IPluginNotImplementedException();
            }
        }

        public IPlugin GetInstanciaDelPlugin()
        {
            return PluginInstance;
        }


    }
}
